class JobController {
    def jobService
    def userService
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ jobInstanceList: Job.list( params ) ]
    }

    def listByCurrentCustomer = {
        if(!params.max) params.max = 10
        [ jobInstanceList: Job.findByCustomer( userService.currentCustomer() ) ]
    }

    def show = {
        def jobInstance = Job.get( params.id )

        if(!jobInstance) {
            flash.message = message(code: 'customer.controller.notfound', args: [params.id])
            redirect(action:list)
        }
        else { return [ jobInstance : jobInstance ] }
    }

    def delete = {
        def jobInstance = Job.get( params.id )
        if(jobInstance) {
            jobInstance.delete()
            flash.message = "Job ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = message(code: 'customer.controller.notfound', args: [params.id])
            redirect(action:list)
        }
    }

    def edit = {
        def jobInstance = Job.get( params.id )

        if(!jobInstance) {
            flash.message = message(code: 'customer.controller.notfound', args: [params.id])
            redirect(action:list)
        }
        else {
            return [ jobInstance : jobInstance ]
        }
    }

    def update = {
        def jobInstance = Job.get( params.id )
        if(jobInstance) {
            jobInstance.properties = params
            if(!jobInstance.hasErrors() && jobService.updateJob(jobInstance)) {
                flash.message = message(code:'job.controller.updated', args:[jobInstance.id])
                redirect(action:show,id:jobInstance.id)
            }
            else {
                render(view:'edit',model:[jobInstance:jobInstance])
            }
        }
        else {
            flash.message = message(code: 'customer.controller.notfound', args: [params.id])
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def jobInstance = new Job()
        jobInstance.properties = params

        if(params.customer.id){
            Customer customer = Customer.get(params.customer.id)
            jobInstance.discount = customer.membership.discount;
        }

        return ['jobInstance':jobInstance]
    }

    def save = {
        def customer = Customer.get(params.customer.id)
        def jobInstance = new Job(params)
        jobInstance.date = new Date()
        //jobInstance.customer =customer
        customer.addToJobs(jobInstance)

        if(!jobInstance.hasErrors() && jobInstance.save()) {
            flash.message = message(code:'job.controller.created', args:[jobInstance.id])
            redirect(action:show,id:jobInstance.id)
        }
        else {
            render(view:'create',model:[jobInstance:jobInstance])
        }
    }

    def process = {
        switch(params.status){
            case Constants.JOB_STATUS_CREATED:
                params.status = Constants.JOB_STATUS_PENDING; break
            case Constants.JOB_STATUS_PENDING:
                params.status = Constants.JOB_STATUS_DONE; break
            case Constants.JOB_STATUS_DONE:
                params.status = Constants.JOB_STATUS_DELIVERED; break
            default:
                params.status = Constants.JOB_STATUS_CREATED

        }
        update()
    }


}
